﻿using Model.DocumentDB;
using Studio.DocumentDB.Dialogs;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Studio.DocumentDB
{
    /// <summary>Tool bar control of the Studio.</summary>
    public partial class ToolBarUserControl : UserControl
    {
        /// <summary>Constructor.</summary>
        public ToolBarUserControl()
        {
            InitializeComponent();
        }

        //  Connect to a new DocumentDB Account
        private async void Connect_Click(object sender, RoutedEventArgs e)
        {
            var studio = DataContext as StudioViewModel;

            if (studio != null)
            {
                var dialog = new AccountCredentialsWindow();

                dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                dialog.Owner = Window.GetWindow(this);

                if (dialog.ShowDialog() == true)
                {
                    studio.Accounts.AddAccount(dialog.Account);
                    await studio.Accounts.SaveAsync();
                }
            }
        }

        private async void Refresh_Click(object sender, RoutedEventArgs e)
        {
            var studio = DataContext as StudioViewModel;

            if (studio != null)
            {
                var refreshable = studio.Accounts.SelectedValue as IRefreshable;

                if (refreshable != null)
                {
                    try
                    {
                        await refreshable.RefreshAsync();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            Window.GetWindow(this),
                            "Refresh:  " + ex.Message,
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                    }
                }
            }
        }

        private void NewDB_Click(object sender, RoutedEventArgs e)
        {
            var studio = DataContext as StudioViewModel;

            if (studio != null)
            {
                var dbFolder = studio.Accounts.SelectedValue;
                //  Find account owning the db folder
                var account = (from a in studio.Accounts
                               where object.ReferenceEquals(dbFolder, a.DatabaseFolder)
                               select a).First();
                var dialog = new NewDbWindow
                {
                    Account = account,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    Owner = Window.GetWindow(this)
                };

                dialog.ShowDialog();
            }
        }

        private async void RemoveDB_Click(object sender, RoutedEventArgs e)
        {
            var studio = DataContext as StudioViewModel;

            if (studio != null)
            {
                var db = studio.Accounts.SelectedValue as DatabaseViewModel;
                //  Find account owning the db
                var account = (from a in studio.Accounts
                               from d in a.DatabaseFolder.FolderContent
                               where object.ReferenceEquals(db, d)
                               select a).First();
                var answer = MessageBox.Show(
                    Window.GetWindow(this),
                    "How you sure you want to destroy database '" + db.Name + "'?",
                    "Confirmation",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question);

                if (answer == MessageBoxResult.Yes)
                {
                    try
                    {
                        var waitWindow = new WaitWindow
                        {
                            WaitingTask = account.DeleteDatabaseAsync(db),
                            Owner = Window.GetWindow(this)
                        };

                        waitWindow.ShowDialog();
                        await waitWindow.WaitingTask;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            Window.GetWindow(this),
                            "Delete DB:  " + ex.Message,
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                    }
                }
            }
        }

        private void NewCollection_Click(object sender, RoutedEventArgs e)
        {
            var studio = DataContext as StudioViewModel;

            if (studio != null)
            {
                var collectionFolder = studio.Accounts.SelectedValue;
                //  Find db owning the collection folder
                var db = (from a in studio.Accounts
                          from d in a.DatabaseFolder.FolderContent
                          where object.ReferenceEquals(collectionFolder, d.CollectionFolder)
                          select d).First();
                var dialog = new NewCollectionWindow
                {
                    Database = db,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner,
                    Owner = Window.GetWindow(this)
                };

                dialog.ShowDialog();
            }
        }

        private async void RemoveCollection_Click(object sender, RoutedEventArgs e)
        {
            var studio = DataContext as StudioViewModel;

            if (studio != null)
            {
                var collection = studio.Accounts.SelectedValue as CollectionViewModel;
                //  Find db owning the collection
                var db = (from a in studio.Accounts
                          from d in a.DatabaseFolder.FolderContent
                          from c in d.CollectionFolder.FolderContent
                          where object.ReferenceEquals(collection, c)
                          select d).First();
                var answer = MessageBox.Show(
                    Window.GetWindow(this),
                    "How you sure you want to destroy collection '" + collection.Name + "'?",
                    "Confirmation",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question);

                if (answer == MessageBoxResult.Yes)
                {
                    try
                    {
                        var waitWindow = new WaitWindow
                        {
                            WaitingTask = db.DeleteCollectionAsync(collection),
                            Owner = Window.GetWindow(this)
                        };

                        waitWindow.ShowDialog();
                        await waitWindow.WaitingTask;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(
                            Window.GetWindow(this),
                            "Delete collection:  " + ex.Message,
                            "Error",
                            MessageBoxButton.OK,
                            MessageBoxImage.Error);
                    }
                }
            }
        }
    }
}